package de.openlaboratory.elib.controller.library;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import de.openlaboratory.elib.controller.ElibService;
import de.openlaboratory.elib.controller.LibraryDAO;
import de.openlaboratory.elib.controller.PublicationDAO;
import de.openlaboratory.elib.core.LogManager;
import de.openlaboratory.elib.model.library.Library;
import de.openlaboratory.elib.model.library.Publication;
import de.openlaboratory.elib.utilities.Operation;

public class LibraryFacade implements ElibService
{

	@Override
	public void destroy()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public ServletConfig getServletConfig()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getServletInfo()
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void init(ServletConfig arg0) throws ServletException
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException
	{
		// TODO Auto-generated method stub

	}

	@Override
	public List<?> execute(String command, Object obj)
	{
		List<Library> libraries = new ArrayList<Library>();
		
		if (command != null && command.toUpperCase().equals("READ"))
		{
			// libraries.add(new LibraryDAO().read((Long) obj));
			Library l = new LibraryDAO().find((Long) obj);
			libraries.add(l);
			LogManager.logMessage("Library loaded [" + obj.toString() + "]");
		}
		else if (command != null && command.toUpperCase().equals("CREATE"))
		{
			Long id = new LibraryDAO().create((Library) obj);
			libraries.add(new LibraryDAO().find(id));
			LogManager.logMessage("Library created [" + id + "]");
		}
		else if (command != null && command.toUpperCase().equals("UPDATE"))
		{
			LibraryDAO dao = new LibraryDAO();
			
			dao.update((Library) obj);
			Library l = dao.find(((Library) obj).getId());
			libraries.add(l);
			LogManager.logMessage("Library updated [" + l.getId().toString() + "]");
		}
		else if (command != null && command.toUpperCase().equals("DELETE"))
		{
			LibraryDAO dao = new LibraryDAO();
			Library l = dao.find((Long) obj);
			dao.delete(l);
			LogManager.logMessage("Library deleted [" + l.getId().toString() + "]");
		}
		else
		{
			libraries = listAllLibraries();
		}
		return libraries;
	}

	public List<Library> listAllLibraries()
	{
		List<Library> libraries = new ArrayList<Library>();
		libraries = new LibraryDAO().findAll();
		
		return libraries;
	}

	@Override
	public String getName() {
		return "Library Service Facade";
	}

	@Override
	public List<?> execute(Operation operation, Object obj) {
		// TODO Auto-generated method stub
		return null;
	}
}
